package udp_client;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.shaolei.basic.BASIC;

public class myGather implements Runnable{
	private static BlockingQueue<byte[]> pkgQ= new LinkedBlockingQueue<byte[]>();
	private static final int IMAGESIZE= BASIC.get_IMAGESIZE2();
	private static final int PKGSIZE= BASIC.get_PKGSIZE();
	private static byte[] myContainer= new byte[IMAGESIZE];
	private static byte[] myContainer2= new byte[IMAGESIZE];
	private static ArrayList<byte[]> myList= new ArrayList<byte[]>();
	private static int myCount=0;
	private static int myCount2=0;
	public void run(){
		myList.add(myContainer);
		myList.add(myContainer2);
		while (true){
			while (pkgQ.isEmpty()==false){
//				System.out.println("myGather::myCount:"+ myCount);
				byte[] SingePkg= pkgQ.peek();
				pkgQ.remove();
				byte[] myID= Arrays.copyOfRange(SingePkg,0, 4);
				int imageID= BASIC.toInt(myID);
				byte[] myStart= Arrays.copyOfRange(SingePkg,4, 8);
				int start_index=BASIC.toInt(myStart);
				byte[] mySize= Arrays.copyOfRange(SingePkg,8, 12);
				int dataSize=BASIC.toInt(mySize);
				byte[] allArray= Arrays.copyOfRange(SingePkg,12, 12+dataSize);
				System.out.println("myGather::SingePkg:"+ imageID+" "+myCount);
				if (imageID%2==0){
					System.arraycopy( allArray, 0, myContainer, start_index, allArray.length );
					myCount++;
				}
				else{
					System.arraycopy( allArray, 0, myContainer2, start_index, allArray.length );
					myCount2++;
				}
			}
		}
	}
	public Queue<byte[]> get_pkgQ(){
		return pkgQ;
	}
	public static byte[] get_myContainer(){
		return myContainer;
	}
	public static byte[] get_myContainer2(){
		return myContainer2;
	}
	public static int get_myCount(){
		return myCount;
	}
	public static int get_myCount2(){
		return myCount2;
	}
	public static void set_myCount(int value){
		myCount=value;
	}
	public static void set_myCount2(int value){
		myCount2=value;
	}
}
